/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.module_main.activity;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.SparseArray;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ldc.commonlibs.config.RouterConfig;
import com.ldc.commonlibs.ui.FragmentBase;
import com.ldc.commonlibs.ui.VActivityBase;
import com.ldc.module_main.R;
import com.ldc.module_main.common.Constants;
import com.ldc.module_main.databinding.MainActivityMainBinding;
import com.ldc.module_main.fragment.FindFragment;
import com.ldc.module_main.fragment.FlowMeFragment;
import com.ldc.module_main.fragment.HomeFragment;
import com.ldc.module_main.fragment.ProfileFragment;
import com.ldc.module_main.viewmodel.SearchViewModel;

@Route(path = RouterConfig.Main.START_ENTER)
public class MainActivity extends VActivityBase<MainActivityMainBinding> {
    private final SparseArray<FragmentBase> pageArray = new SparseArray<>();
    private FragmentBase currentFragmennt = new FindFragment();

    @Override
    protected void main(@Nullable Bundle savedInstanceState) {
        initListener();
    }

    @Override
    protected MainActivityMainBinding initViewBinding() {
        return MainActivityMainBinding.inflate(getLayoutInflater());
    }

    private void initListener() {
        initPage();
        viewBinding.topBar.llSearch.setOnClickListener(v -> {
            SearchActivity.start(this);
        });
    }

    private void initPage() {
        changePage(Constants.HOME_PAGE);
        viewBinding.bottomBar.flHome.setOnClickListener(v -> {
            changePage(Constants.HOME_PAGE);
        });
        viewBinding.bottomBar.flHome2.setOnClickListener(v -> {
            changePage(Constants.FIND_PAGE);
        });
        viewBinding.bottomBar.flHome3.setOnClickListener(v -> {
            changePage(Constants.FLOW_ME_PAGE);
        });
        viewBinding.bottomBar.flHome4.setOnClickListener(v -> {
            changePage(Constants.PROFILE_PAGE);
        });
    }

    private void changePage(int code) {
        changePage(getPage(code));
    }

    private void changePage(FragmentBase target) {
        if (target == null) {
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (currentFragmennt != target) {
            if (target.isAdded()) {
                fragmentTransaction.hide(currentFragmennt)
                        .show(target)
                        .commit();
            } else {
                fragmentTransaction.add(R.id.fl_container, target)
                        .hide(currentFragmennt)
                        .show(target)
                        .commit();
            }
            currentFragmennt = target;

        }
    }

    private FragmentBase getPage(int code) {
        FragmentBase fragmentBase = pageArray.get(code);
        if (fragmentBase == null) {
            switch (code) {
                case Constants.HOME_PAGE:
                    fragmentBase = HomeFragment.newInstance();
                    pageArray.put(code, fragmentBase);
                    break;
                case Constants.FIND_PAGE:
                    fragmentBase = FindFragment.newInstance();
                    pageArray.put(code, fragmentBase);
                    break;
                case Constants.FLOW_ME_PAGE:
                    fragmentBase = FlowMeFragment.newInstance();
                    pageArray.put(code, fragmentBase);
                    break;
                case Constants.PROFILE_PAGE:
                    fragmentBase = ProfileFragment.newInstance();
                    pageArray.put(code, fragmentBase);
                    break;
                default:
                    break;
            }
        }
        return fragmentBase;

    }

}